package com.github.catvod.spider;

import android.text.TextUtils;

import com.github.catvod.bean.Class;
import com.github.catvod.bean.Filter;
import com.github.catvod.bean.Result;
import com.github.catvod.bean.Vod;
import com.github.catvod.crawler.Spider;
import com.github.catvod.crawler.SpiderDebug;
import com.github.catvod.net.OkHttp;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Objects;

public class Xldy extends Spider {

    private final String url = "https://www.xlys01.com";

    private Filter getFilter(String name, String key, Elements typeList) {
        List<Filter.Value> values = new ArrayList<>();
        if ("fl".equals(key)) {
            values.add(new Filter.Value("不限",  ""));
            values.add(new Filter.Value("电影",  "0"));
            values.add(new Filter.Value("电视剧",  "1"));
        }
        if ("type".equals(key)) {
            values.add(new Filter.Value("不限",  "/s/all"));
            values.add(new Filter.Value("动作",  "/s/dongzuo"));
            values.add(new Filter.Value("爱情",  "/s/aiqing"));
            values.add(new Filter.Value("喜剧",  "/s/xiju"));
            values.add(new Filter.Value("科幻",  "/s/kehuan"));
            values.add(new Filter.Value("恐怖",  "/s/kongbu"));
            values.add(new Filter.Value("战争",  "/s/zhanzheng"));
            values.add(new Filter.Value("武侠",  "/s/wuxia"));
            values.add(new Filter.Value("魔幻",  "/s/mohuan"));
            values.add(new Filter.Value("剧情",  "/s/juqing"));
            values.add(new Filter.Value("动画",  "/s/donghua"));
            values.add(new Filter.Value("惊悚",  "/s/jingsong"));
            values.add(new Filter.Value("3D",  "/s/3D"));
            values.add(new Filter.Value("灾难",  "/s/zainan"));
            values.add(new Filter.Value("悬疑",  "/s/xuanyi"));
            values.add(new Filter.Value("警匪",  "/s/jingfei"));
            values.add(new Filter.Value("文艺",  "/s/wenyi"));
            values.add(new Filter.Value("青春",  "/s/qingchun"));
            values.add(new Filter.Value("冒险",  "/s/maoxian"));
            values.add(new Filter.Value("犯罪",  "/s/fanzui"));
            values.add(new Filter.Value("纪录",  "/s/jilu"));
            values.add(new Filter.Value("古装",  "/s/guzhuang"));
            values.add(new Filter.Value("奇幻",  "/s/qihuan"));
            values.add(new Filter.Value("国语",  "/s/guoyu"));
            values.add(new Filter.Value("综艺",  "/s/zongyi"));
            values.add(new Filter.Value("历史",  "/s/lishi"));
            values.add(new Filter.Value("运动",  "/s/yundong"));
            values.add(new Filter.Value("原创压制",  "/s/yuanchuang"));
            values.add(new Filter.Value("美剧",  "/s/meiju"));
            values.add(new Filter.Value("韩剧",  "/s/hanju"));
            values.add(new Filter.Value("国产电视剧",  "/s/guoju"));
            values.add(new Filter.Value("日剧",  "/s/riju"));
            values.add(new Filter.Value("英剧",  "/s/yingju"));
            values.add(new Filter.Value("德剧",  "/s/deju"));
            values.add(new Filter.Value("俄剧",  "/s/eju"));
            values.add(new Filter.Value("巴剧",  "/s/baju"));
            values.add(new Filter.Value("加剧",  "/s/jiaju"));
            values.add(new Filter.Value("西剧",  "/s/spanish"));
            values.add(new Filter.Value("意大利剧",  "/s/yidaliju"));
            values.add(new Filter.Value("泰剧",  "/s/taiju"));
            values.add(new Filter.Value("港台剧",  "/s/gangtaiju"));
            values.add(new Filter.Value("法剧",  "/s/faju"));
            values.add(new Filter.Value("澳剧",  "/s/aoju"));
        }

        if ("area".equals(key)) {
            values.add(new Filter.Value("不限", ""));
            values.add(new Filter.Value("中国大陆", "%E4%B8%AD%E5%9B%BD%E5%A4%A7%E9%99%86"));
            values.add(new Filter.Value("中国香港", "%E4%B8%AD%E5%9B%BD%E9%A6%99%E6%B8%AF"));
            values.add(new Filter.Value("中国台湾", "%E4%B8%AD%E5%9B%BD%E5%8F%B0%E6%B9%BE"));
            values.add(new Filter.Value("美国", "%E7%BE%8E%E5%9B%BD"));
            values.add(new Filter.Value("英国", "%E8%8B%B1%E5%9B%BD"));
            values.add(new Filter.Value("日本", "%E6%97%A5%E6%9C%AC"));
            values.add(new Filter.Value("韩国", "%E9%9F%A9%E5%9B%BD"));
            values.add(new Filter.Value("法国", "%E6%B3%95%E5%9B%BD"));
            values.add(new Filter.Value("印度", "%E5%8D%B0%E5%BA%A6"));
            values.add(new Filter.Value("德国", "%E5%BE%B7%E5%9B%BD"));
            values.add(new Filter.Value("西班牙", "%E8%A5%BF%E7%8F%AD%E7%89%99"));
            values.add(new Filter.Value("意大利", "%E6%84%8F%E5%A4%A7%E5%88%A9"));
            values.add(new Filter.Value("澳大利亚", "%E6%BE%B3%E5%A4%A7%E5%88%A9%E4%BA%9A"));
            values.add(new Filter.Value("比利时", "%E6%AF%94%E5%88%A9%E6%97%B6"));
            values.add(new Filter.Value("瑞典", "%E7%91%9E%E5%85%B8"));
            values.add(new Filter.Value("荷兰", "%E8%8D%B7%E5%85%B0"));
            values.add(new Filter.Value("丹麦", "%E4%B8%B9%E9%BA%A6"));
            values.add(new Filter.Value("加拿大", "%E5%8A%A0%E6%8B%BF%E5%A4%A7"));
            values.add(new Filter.Value("俄罗斯", "%E4%BF%84%E7%BD%97%E6%96%AF"));
        }

        if ("year".equals(key)) {
            values.add(new Filter.Value("不限", ""));
            values.add(new Filter.Value("2024", "2024"));
            values.add(new Filter.Value("2023", "2023"));
            values.add(new Filter.Value("2022", "2022"));
            values.add(new Filter.Value("2021", "2021"));
            values.add(new Filter.Value("2020", "2020"));
            values.add(new Filter.Value("2019", "2019"));
            values.add(new Filter.Value("2018", "2018"));
            values.add(new Filter.Value("2017", "2017"));
            values.add(new Filter.Value("2016", "2016"));
            values.add(new Filter.Value("2015", "2015"));
            values.add(new Filter.Value("2014", "2014"));
            values.add(new Filter.Value("2013", "2013"));
            values.add(new Filter.Value("2012", "2012"));
            values.add(new Filter.Value("2011", "2011"));
            values.add(new Filter.Value("2010", "2010"));
            values.add(new Filter.Value("2009", "2009"));
            values.add(new Filter.Value("2008", "2008"));
            values.add(new Filter.Value("2007", "2007"));
            values.add(new Filter.Value("2006", "2006"));
            values.add(new Filter.Value("2005", "2005"));
            values.add(new Filter.Value("2004", "2004"));
            values.add(new Filter.Value("2003", "2003"));
            values.add(new Filter.Value("2002", "2002"));
            values.add(new Filter.Value("2001", "2001"));
        }

        if ("by".equals(key)) {
            values.add(new Filter.Value("不限", ""));
            values.add(new Filter.Value("更新时间", "0"));
            values.add(new Filter.Value("豆瓣评分", "1"));
        }
        return new Filter(key, name, values);
    }

    @Override
    public String homeContent(boolean filter) throws Exception {
        List<Class> classes = new ArrayList<>();
        List<Vod> list = new ArrayList<>();
        LinkedHashMap<String, List<Filter>> filters = new LinkedHashMap<>();
        Document doc = Jsoup.parse(OkHttp.string(url));
        for (Element li : doc.select("header").get(1).select("a")) {
            String typeName = li.select("a").text();
            String typeId = li.select("a").attr("href");
            classes.add(new Class(typeId, typeName));
            List<Filter> array = new ArrayList<>();
            array.add(getFilter("地区", "area", null));
            array.add(getFilter("影视类型", "type", null));
            array.add(getFilter("上映时间", "year", null));
            array.add(getFilter("影视排序", "by", null));
            array.add(getFilter("分类", "fl", null));
            filters.put(typeId, array);
        }
        for (Element div : doc.select(".container").select(".card")) {
            String id = div.select("a").attr("href");
            String name = div.select(".card-title").text();
            String pic = div.select("img").attr("data-src");
            String tag = div.select(".ribbon").text();
            String remark = div.select("p.text-muted").text();
            Vod vod = new Vod(id, name, pic, tag + "/" + remark);
            list.add(vod);
        }
        return Result.string(classes, list, filters);
    }

    @Override
    public String categoryContent(String tid, String pg, boolean filter, HashMap<String, String> extend) throws Exception {
        SpiderDebug.log(tid);
        List<Vod> list = new ArrayList<>();
        if (extend.isEmpty()) {
            extend = new HashMap<>();
        }
        String urlP = "";
        String param = "?1=1";
        if (extend.containsKey("lx") && !(extend.get("lx").isEmpty())) {
            param += "&type=" + extend.get("lx");
        }
        if (extend.containsKey("type") && !(extend.get("type").isEmpty())) {
            urlP = url + extend.get("type");
        }
        if (extend.containsKey("year") && !(extend.get("year").isEmpty())) {
            param+= "&year=" + extend.get("year");
        }
        if (extend.containsKey("area") && !(extend.get("area").isEmpty())) {
            param+= "&area=" + extend.get("area");
        }
        if (extend.containsKey("by") && !(extend.get("by").isEmpty())) {
            param+= "&order=" + extend.get("by");
        }
        String target = urlP.isEmpty() ? url + tid + "/" + pg + param : urlP + "/" + pg + param;
        SpiderDebug.log(target);
        Document doc = Jsoup.parse(OkHttp.string(target.replace("1=1&", "").replace("?1=1", "")));
        SpiderDebug.log(doc.html());
        for (Element div : doc.select(".row-cards").select(".card")) {
            String id = div.select("a").attr("href");
            String name = div.select(".card-title").text();
            String pic = div.select("img").attr("data-src");
            String tag = div.select(".ribbon").text();
            String remark = div.select("p.text-muted").text();
            Vod vod = new Vod(id, name, pic, tag + "/" + remark);
            list.add(vod);
        }
        return Result.string(list);
    }

    @Override
    public String detailContent(List<String> ids) throws Exception {
        SpiderDebug.log(ids.get(0));
        String html = OkHttp.string(url + ids.get(0));
        SpiderDebug.log(html);
        Document doc = Jsoup.parse(html);
        String name = doc.select("div.card-body").get(0).select("h2").text();
        Elements sourceList = doc.select("#play-list").select("a");
        List<String> vodItems = new ArrayList<>();
        for (Element a : sourceList) {
            String episodeUrl = a.attr("href");
            String episodeName = a.text();
            vodItems.add(episodeName + "$" + url + episodeUrl);
        }
        Vod vod = new Vod();
        vod.setVodId(ids.get(0));
        vod.setVodDirector(doc.select("div.card-body").get(0).select("row").get(1).select("div").get(1).select("p").get(1).select("a").text());
        vod.setVodActor(doc.select("div.card-body").get(0).select("row").get(1).select("div").get(1).select("p").get(3).select("a").text());
        vod.setVodPic(doc.select("div.card-body").get(0).select("row").get(1).select("img").attr("src"));
        vod.setVodName(name);
        vod.setVodPlayFrom("修罗");
        vod.setVodContent(doc.select("div.card-body").get(1).text());
        vod.setVodPlayUrl(TextUtils.join("#", vodItems));
        vod.setVodArea(doc.select("div.card-body").get(0).select("row").get(1).select("div").get(1).select("p").get(5).text());
        vod.setVodYear(doc.select(".bg-purple-lt").text());
        vod.setVodRemarks(doc.select(".d-md-block").get(1).select("span").text());
        return Result.string(vod);
    }

    @Override
    public String playerContent(String flag, String id, List<String> vipFlags) throws Exception {
        return Result.get().url(id).parse().string();
    }

    @Override
    public boolean manualVideoCheck() throws Exception {
        return true;
    }

    @Override
    public boolean isVideoFormat(String url) throws Exception {
        return url.contains("?x-expires=") && url.contains("&x-signature=");
    }

    @Override
    public String searchContent(String key, boolean quick) throws Exception {
        return searchContent("/search/" + key);
    }

    @Override
    public String searchContent(String key, boolean quick, String pg) throws Exception {
        return searchContent("/search/" + key + "/" + pg);
    }

    private String searchContent(String query) {
        List<Vod> list = new ArrayList<>();
        Document doc = Jsoup.parse(OkHttp.string(url + query));
        for (Element div : doc.select("card-body")) {
            String id = div.select("a").attr("href");
            String name = div.select("a").text();
            String pic = div.select("img").get(0).attr("src");
            Vod vod = new Vod(id, name, pic);
            list.add(vod);
        }
        return Result.string(list);
    }
}

